package com.wm.wmim.common.bo.resp;

import com.wm.wmim.common.constant.HttpStatusConstant;
import com.wm.wmim.common.dto.RpcRespDTO;
import com.wm.wmim.common.enums.CommonBizCodeEnum;
import com.wm.wmim.common.utils.RpcRespUtils;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;


import java.io.Serializable;


@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class CommonBizRespBo<T> implements Serializable {

    private static final long serialVersionUID = -768486029533776811L;

    private static final Integer SUCCESS_SERIES = 2;

    private static final Integer CLIENT_ERROR_SERIES = 4;

    private static final Integer SERVER_ERROR_SERIES = 5;

    /**
     * 业务响应码
     */
    private String bizRestCode;

    /**
     * 业务响应消息
     */
    private String bizRestMsg;
    /**
     * 详细响应消息
     */
    private String bizRestDetail;

    /**
     * 获取HTTP状态码，该业务状态码在此种HTTP状态码下会返回
     * @return Integer 状态码，参考 org.springframework.http.HttpStatus 中的定义
     */
    private Integer httpStatus;

    /**
     * 业务响应体
     */
    private T data;
    
    public CommonBizRespBo(String bizRestCode, String bizRestMsg, String bizRestDetail, T data) {
        this.bizRestCode = bizRestCode;
        this.bizRestMsg = bizRestMsg;
        this.bizRestDetail = bizRestDetail;
        this.data = data;
    }

    public boolean isSuccess(){
        return StringUtils.equals(CommonBizCodeEnum.SUCCESS.getCode(), this.getBizRestCode());
    }


    /**
     * 构造错误业务操作响应体
     *
     * @param <T> 业务响应类型
     * @return CommonBizRespBo 响应对象
     */
    public static <T> CommonBizRespBo<T> buildErrorResp(CommonBizCodeEnum codeEnum) {
        return new CommonBizRespBo<>(codeEnum.getCode(), codeEnum.getMsg(), null,
                codeEnum.getHttpStatus(), null);
    }

    /**
     * 构造错误业务操作响应体
     *
     * @param <T> 业务响应类型
     * @return CommonBizRespBo 响应对象
     */
    public static <T> CommonBizRespBo<T> buildErrorResp() {
        return new CommonBizRespBo<>(CommonBizCodeEnum.ERROR_UNKNOWN.getCode(), CommonBizCodeEnum.ERROR_UNKNOWN.getMsg(),
                null, HttpStatusConstant.INTERNAL_SERVER_ERROR, null);
    }



    /**
     * 构造成功业务操作响应体
     *
     * @param data 业务响应体
     * @param <T>  业务响应体类型
     * @return 响应对象
     */
    public static <T> CommonBizRespBo<T> buildSuccessResp(T data) {
        return new CommonBizRespBo<>(CommonBizCodeEnum.SUCCESS.getCode(), CommonBizCodeEnum.SUCCESS.getMsg(), null,
                HttpStatusConstant.OK, data);
    }

    /**
     * 构造成功业务操作响应体
     *
     * @param <T> 业务响应类型
     * @return 响应对象
     */
    public static <T> CommonBizRespBo<T> buildSuccessRespWithoutData() {
        return new CommonBizRespBo<>(CommonBizCodeEnum.SUCCESS.getCode(), CommonBizCodeEnum.SUCCESS.getMsg(), null,
                HttpStatusConstant.OK, null);
    }

    /**
     * 构造业务操作响应体
     *
     * @param code 响应code
     * @param msg  响应消息
     * @param data 业务响应体
     * @param <T>  业务响应类型
     * @return 响应对象
     */
    public static <T> CommonBizRespBo<T> buildResp(String code, String msg, T data) {
        return new CommonBizRespBo<>(code, msg, null, null, data);
    }


    /**
     * 构造RPC响应
     *
     * @return RPC响应上
     */
    public RpcRespDTO<T> buildRpcResponse() {
        return RpcRespUtils.buildResp(this.getBizRestCode(), this.getBizRestMsg(), this.getBizRestDetail(),
                this.getData(), this.getHttpStatus());
    }

}